package com.season.seasonStudy.javase.j_006_recursion;

import java.util.Scanner;
/**
 * 在递归中,每一次都需要return退出去,
 * 
 * 因为退出去的是当前的方法,所以呢	每一次都要递归,都要return
 * 
 * 树状展现需要递归
 */

/**
 * 递归,就像是栈一样,先进后出,能够保证每一次进入的都能控制开始和结束
 *
 */
public class DiguiDemo {
	public static void main(String[] args) {
		/**
		 * F1=1   F2=1
		 * Fn = Fn-1 + Fn-2;
		 * 求F40
		 * Febonacci数列
		 */
		for (int i = 0;; i++) {
			System.out.println("请输入数字");
			Scanner scanner = new Scanner(System.in);
			System.out.println(fibonacci(scanner.nextInt()));
		}	
	}

	public static int fibonacci(int end){
		if (end<=0) {
			return -1;
		}else if (end==1 || end==2) {
			return 1;
		}else {
			return fibonacci(end-1)+fibonacci(end-2);
		}
	}
	
	/**
	 * 在这里用非递归做的上述问题,但是看起来没上面好懂
	 */
	public static void main2(String[] args) {
		int answer;
		for (;;) {
			System.out.println("-----------");
			Scanner scanner = new Scanner(System.in);
			System.out.println(num(scanner.nextInt()));
		}
	}
	public static int num(int time) {
		if(time ==1 || time==2){
			return 1;
		}else if (time >= 3) {
			int num1=1;
			int num2=1;
			for (int i = 3; i <= time; i++) {
				int tmp = num1 + num2;
				num1=num2;
				num2 = tmp;
			}
			return num2;
		}else {
			return 0;
		}
	}

}

